<template>
  <a-modal :title="title" :width="width" :visible="visible" @ok="handleOk" :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }" @cancel="handleCancel" cancelText="关闭">
    <CommitDefectForm ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false"></CommitDefectForm>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref, nextTick, defineExpose ,reactive} from 'vue';
  import CommitDefectForm from './CommitDefectForm.vue'
  
  const title = ref<string>('');
  const width = ref<number>(1000);
  const visible = ref<boolean>(false);
  const disableSubmit = ref<boolean>(false);
  const registerForm = ref();
  const emit = defineEmits(['register', 'success']);
  const formData = reactive<Record<string, any>>({
    id: '',
    projectId: '',   
    batchId: '',   
    systemId: '',   
    caseId: '',
    systemModuleId: '',   
    functionName: '',  
    defectLevel: '',   
    defectType: '',   
    defectTitle: '',   
    description: '',  
    testStage: '',   
    defectImg: '',   
    envType: '',   
    defectPriority: '',   
    bugStatus: '1',   
    solverId: '',      
  });
  /**
   * 新增
   */
  function addDefect(record) {
    title.value = '提交缺陷';
    visible.value = true;
    nextTick(() => {
      formData.projectId = record.projectId;
      formData.systemId = record.systemId;
      formData.batchId = record.batchId;
      formData.caseId = record.id;
      formData.defectTitle = record.defectTitle;
      formData.functionName = record.functionName;
      formData.description = record.description;
      formData.defectImg = record.defectImg;
      registerForm.value.addDefect(formData);
    });
  }
  
  /**
   * 编辑
   * @param record
   */
  function edit(record) {
    title.value = disableSubmit.value ? '详情' : '编辑';
    visible.value = true;
    nextTick(() => {
      registerForm.value.edit(record);
    });
  }
  
  /**
   * 确定按钮点击事件
   */
  function handleOk() {
    registerForm.value.submitForm();
  }

  /**
   * form保存回调事件
   */
  function submitCallback() {
    handleCancel();
    emit('success');
  }

  /**
   * 取消按钮回调事件
   */
  function handleCancel() {
    visible.value = false;
  }

  defineExpose({
    addDefect,
    edit,
    disableSubmit,
  });
</script>

<style>
  /**隐藏样式-modal确定按钮 */
  .jee-hidden {
    display: none !important;
  }
</style>
